package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApAuthor;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeignClient;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-20
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Autowired
    IApUserService userService;

    @Override
    public ResponseResult listByStatus(AuthDto dto) {
        //分页
        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        //查询条件
        LambdaQueryWrapper<ApUserRealname> query = new LambdaQueryWrapper<>();
        if (dto.getStatus() != null) {
            query.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        IPage<ApUserRealname> page1 = this.page(page, query);

        //响应分页
        PageResponseResult responseResult=new PageResponseResult(dto.getPage(),dto.getSize(),page1.getTotal(),page1.getRecords());

        return responseResult;
    }

    /**
     * 审核 1:成功 ,0:失败
     * @param authDto
     * @param i
     * @return
     */
    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto authDto, int i) {
        //获取认证表
        ApUserRealname userRealname = this.getById(authDto.getId());
        //审核不通过
        if (i == 0){
            userRealname.setStatus(2);
            userRealname.setReason(authDto.getReason());
            this.updateById(userRealname);
        return ResponseResult.okResult();
        }
        //审核通过


        //获取用户注册表
        ApUser apuser = userService.getById(userRealname.getUserId());
        //创建自媒体用户
        WmUser wmUser=createWmUser(apuser);
        if (wmUser !=null){
            //创建作者账号
            ApAuthor apAuthor=createApAuthor(apuser,wmUser);
            if (apAuthor !=null){
                //修改用户认证状态
                userRealname.setStatus(9);
                this.updateById(userRealname);
                //修改用户注册表的flag 为1 自媒体人
                apuser.setFlag(1);
                userService.updateById(apuser);
            }
        }
        return ResponseResult.okResult();
    }


    @Autowired
    private ArticleFeign articleFeign;
    /**
     * 创建文章作者账号
     * @param apuser
     * @param wmUser
     * @return
     */
    private ApAuthor createApAuthor(ApUser apuser, WmUser wmUser) {
        //创建文章作者账号
        ApAuthor apAuthor=new ApAuthor();
        apAuthor.setName(apuser.getName());
        apAuthor.setUserId(apuser.getId());
        apAuthor.setWmUserId(wmUser.getId());
        apAuthor.setType(2);
        //经行远程调用
        ResponseResult<ApAuthor> apAuthorResponseResult = articleFeign.saveApAuthor(apAuthor);
        //判断远程调用是否正确
        if (apAuthorResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())){
            ApAuthor data = apAuthorResponseResult.getData();
            //更新自媒体账号
            if (data !=null){
                wmUser.setApAuthorId(apAuthor.getId());
                mediaFeignClient.updateWmuser(wmUser);
            }
            return data;
        }
        return null;
    }


    @Autowired
    private MediaFeignClient mediaFeignClient;
    /**
     * 创建自媒体账号
     * @param apUser
     * @return
     */
    private WmUser createWmUser(ApUser apUser) {
        //创建wmuser自媒体表
        WmUser wmUser=new WmUser();
        //属性拷贝
        BeanUtils.copyProperties(apUser,wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(apUser.getId());
        wmUser.setNickname(apUser.getName());
        wmUser.setStatus(9);
        wmUser.setType(0);
        //发起调用
        ResponseResult<WmUser> saveWmUser = mediaFeignClient.saveWmUser(wmUser);
        if (saveWmUser.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())){
            WmUser data = saveWmUser.getData();
            return data;
        }
        return null;
    }
}
